//
//  DULanguage.m
//  language
//
//  Created by duyujin1 on 2021/8/1.
//

#import "DULanguage.h"
#import "NSBundle+language.h"

@implementation DULanguage

+(DULanguage *) sharedInstance{
    static DULanguage * instance=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance=[[DULanguage alloc] init];
    });
    return instance;
}

-(void)initLanguage{
    NSString * language=[self currentLanguage];
    if(language.length>0){
        NSLog(@"自设置语言：%@",language);
    }else{
        [self systemLanguage];
    }
}

-(NSString *)currentLanguage{
    NSString * language=[[NSUserDefaults standardUserDefaults] objectForKey:AppLanguage];
    return language;
}

- (void)setLanguage:(NSString *)language{
    [NSBundle setLanguage:language];
    [[NSUserDefaults standardUserDefaults] setObject:language forKey:AppLanguage];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(void)systemLanguage{
    NSString * languageCode=[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"][0];
    NSLog(@"系统语言：%@",languageCode);
    if([languageCode hasPrefix:@"zh-Hans"]){
        languageCode=@"zh-Hans";
    }else if([languageCode hasPrefix:@"en"]){
        languageCode=@"en";
    }
    [self setLanguage:languageCode];
}



@end
